<?php
namespace app\common\model;
use app\common\model\Base;
class Order extends Base
{
    protected $resultSetType = 'collection';

    static $status = [
                1 => '待发货',
                2 => '待签收',
                3 => '订单完成',
                0 => '订单取消',
            ];

    public function status($key=null){
        if(!is_null($key)){
            return self::$status[$key];
        }else{
            return self::$status;
        }
    }
    /**
     * [addOrder 插入订单]
     * @param [type] $data [description]
     */
    public function addOrder($data){
        return $this->insert($data);
    }

    /**
     * [updateOrder 修改订单]
     * @param  array  $where [description]
     * @param  array  $data  [description]
     * @return [type]        [description]
     */
    public function updateOrder($where=[],$data=[]){
        return $this->where($where)->update($data);
    }

    public function getPageDatas($where,$page,$perpagenum,$field=''){
        $start = ($page-1)*$perpagenum;
        $whereData=[];
        if(!empty($where)){
            foreach($where as $k => $wh):
                $whereData['a.'.$k] = $wh;
            endforeach;
        }
        $fields = '';
        if(!empty($field)){
            $arr = explode(',',$field);
            foreach($arr as $v):
                $fields .= 'a.'.$v.',';
            endforeach;
        }
        $fields .= 'b.mid';
        $rows = $this
              ->alias('a')
              ->join('member b','b.id = a.uid')
              ->where($whereData)
              ->limit($start,$perpagenum)
              ->order('a.id','DESC')
              ->field($fields)
              ->select();
        $count = $this->alias('a')->where($whereData)->count();
        $data = [];
        if($count){
            foreach($rows as $row):
                $row = $row->toArray();
                $data[] = $this->setField($row);
            endforeach;
        }
        $total_page = ceil($count/$perpagenum);
        return [
            'total_page' => $total_page,
            'curent_page' => $page,
            'count' => $count,
            'data' => $data
        ];
    }

    /**
     * 查询一条订单
     */
    public function getOneDatas($where=[]){
        $data = $this
              ->alias('a')
              ->join('member b','b.id = a.uid')
              ->field('a.*,b.mid')
              ->where($where)
              ->find();
        $row = $this->setField($data);
        return $data;
    }

    /**
     * 查询多条订单不做数据处理
     */
    public function getAllRow($where=[],$fields='id,order_id,uid,logis_id,status,credit'){
        $rows = $this->where($where)->field($fields)->select()->toArray();
        foreach($rows as $row):
                $data[] = $this->setField($row);
        endforeach;
        return $data;
    }

    /**
     * 处理字段数据
     * @param [type] $row [description]
     */
    public function setField($row){
        if(isset($row['des'])){
            $row['des'] = unserialize($row['des']);
        }
        if(isset($row['uid'])){
            $row['mid'] = model('Member')->where(['id'=>$row['uid']])->value('mid');
        }
        if(isset($row['create_time'])){
            $row['create_time'] = date("Y-m-d H:i",$row['create_time']);
        }
        if(isset($row['address_id'])){
            $row['userinfo'] =model('MemberAddress')->memberAddressFind($row['address_id']);
        }
        if(isset($row['status'])){
            switch($row['status']){
                case '1':
                   $row['chs_status'] = '待发货';
                    break;
                case '2':
                   $row['chs_status'] = '待签收';
                    break;
                case '3':
                   $row['chs_status'] = '订单完成';
                    break;
                default:
                   $row['chs_status'] = '订单取消';
                    break;
            }
        }
        if(isset($row['order_id'])){
             $row['goods']=model('OrderDetails')->getDatas(['order_id'=>$row['order_id']]);
             $logis_no =model('OrderLogistics')->getOneDatas(['logis_order_id'=>$row['order_id']],'logis_no');
             $row['logis_no'] = $logis_no['logis_no'];
        }
        return $row;
    }


    /**
     * [orderMemberCoinRecord 添加资金明细]
     * @param  string $type          [description]
     * @param  string $mid           [description]
     * @param  string $coin_id       [description]
     * @param  string $change_amount [description]
     * @param  string $msg           [description]
     * @return [type]                [description]
     */
    public function orderMemberCoinRecord($type='',$uid='',$coin_id='',$change_amount='',$msg=''){
        $where = ['uid'=>$uid,'coin_id'=>$coin_id];
        $beforeAmount = model('MemberCoin')->where($where)->value('amount');
        $operate = '';
        if($type=='dec'){
            $operate = 'buy';
            model('MemberCoin')->where($where)->setDec('amount',$change_amount);
        }else{
            $operate = 'rejected';
            model('MemberCoin')->where($where)->setInc('amount',$change_amount);
        }
        $afterAmount = model('MemberCoin')->where($where)->value('amount');
        $record = [
                    'operate_id' => 0,
                    'uid' => $uid,
                    'coin_id' => $coin_id,
                    'amount' => $change_amount,
                    'operate' => $operate,
                    'before_amount' => $beforeAmount,
                    'after_amount' => $afterAmount,
                    'message' => '',
                    'remark' => $msg,
                    'flag' => $type
                ];
        model('MemberCoinRecord')->save($record);
        return true;
    }

}
